package Designation;

import connectDB.ConnectDB;
import java.sql.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class Des_View extends javax.swing.JFrame {

    ConnectDB connect = new ConnectDB();

    public Des_View() {
        connect.getConnect();
        initComponents(); //tạo giao diện
        loadTableDes();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblDes = new javax.swing.JTable();
        btnDesDelete = new javax.swing.JButton();
        btnDesUpdate = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tblDes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tblDes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblDesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblDes);

        btnDesDelete.setText("Delete");
        btnDesDelete.setEnabled(false);
        btnDesDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDesDeleteActionPerformed(evt);
            }
        });

        btnDesUpdate.setText("Update");
        btnDesUpdate.setEnabled(false);
        btnDesUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDesUpdateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(btnDesUpdate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnDesDelete))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 16, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDesUpdate)
                    .addComponent(btnDesDelete))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblDesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblDesMouseClicked
        // TODO add your handling code here:
        //JOptionPane.showMessageDialog(this, "aaaaa");
        btnDesDelete.setEnabled(true);
        btnDesUpdate.setEnabled(true);
    }//GEN-LAST:event_tblDesMouseClicked

    private void btnDesDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDesDeleteActionPerformed
        deleteDes();
    }//GEN-LAST:event_btnDesDeleteActionPerformed

    private void btnDesUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDesUpdateActionPerformed

        int choice = tblDes.getSelectedRow();
        String id = tblDes.getValueAt(choice, 0).toString();
        String name = tblDes.getValueAt(choice, 1).toString();
        Des_Update up = new Des_Update(id);
        up.setVisible(true);
        loadTableDes();
    }//GEN-LAST:event_btnDesUpdateActionPerformed

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Des_View().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDesDelete;
    private javax.swing.JButton btnDesUpdate;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblDes;
    // End of variables declaration//GEN-END:variables

    public void loadTableDes() {
        try {
            String query = "Select * From Designation";
            Statement st = connect.cn.createStatement();
            ResultSet rs = st.executeQuery(query);
            ResultSetMetaData rsmd = rs.getMetaData();
            int numCol = rsmd.getColumnCount();
            Vector nameCol = new Vector();


            for (int i = 1; i <= numCol; i++) {
                nameCol.add(rsmd.getColumnName(i));
            }

            DefaultTableModel dtm = new DefaultTableModel(nameCol, 0);

            while (rs.next()) {

                Vector dataRow = new Vector();
//                dataRow.add(rs.getString("DESIGNID"));
//                dataRow.add(rs.getString("DESIGNATION"));
                for (int i = 1; i <= numCol; i++) {
                    dataRow.add(rs.getString(i));
                }
                dtm.addRow(dataRow);
            }
            tblDes.setModel(dtm);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void deleteDes() {
        int j = JOptionPane.showConfirmDialog(this, "Are you sure?", "Yes or No", 0);
        if (j == JOptionPane.YES_OPTION) {
            try {

                String query = "Delete Designation where DESIGNID = ? ";

                PreparedStatement pst = connect.cn.prepareStatement(query);
                int choice = tblDes.getSelectedRow();
                String id = tblDes.getValueAt(choice, 0).toString();
                pst.setString(1, id);
                if (pst.executeUpdate() == -1) {
                    JOptionPane.showMessageDialog(this, "Cannot delete");

                } else {
                    JOptionPane.showMessageDialog(this, "Delete successfully");
                    loadTableDes();
                    this.dispose();
                }

            } catch (Exception ex) {
                //ex.printStackTrace();
                JOptionPane.showMessageDialog(this, "Delete fail");
            }
        }
        if (j == JOptionPane.NO_OPTION) {
            JOptionPane.showMessageDialog(this, "Please choose the object that you want to delete, again!!!");
            this.dispose();
        }
    }
}
